package com.magictower.primary.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.magictower.primary.R;

/**
 * 设置自定义组件
 * Created by kejunzhan on 2017/12/6.
 */

public class ItemView extends RelativeLayout {
    private static final String NAMESPACE = "http://schemas.android.com/apk/res-auto";
    private TextView tvTitle;
    private TextView tvDesc;
    private CheckBox cbStatus;
    private String mTitle;
    private String mDescOn;
    private String mDescOff;


    public ItemView(Context context) {
        super(context);

        initView();
    }

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //根据空间名及属性名进行获取
        mTitle = attrs.getAttributeValue(NAMESPACE, "title");
        mDescOn = attrs.getAttributeValue(NAMESPACE, "desc_on");
        mDescOff = attrs.getAttributeValue(NAMESPACE, "desc_off");
        initView();
    }

    public ItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        View.inflate(getContext(), R.layout.view_item, this);

        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvDesc = (TextView) findViewById(R.id.tv_desc);
        cbStatus = (CheckBox) findViewById(R.id.cb_status);

        setTvTitle(mTitle);
    }

    private void setTvTitle(String title) {
        tvTitle.setText(title);
    }

    private void setTvDesc(String desc) {
        tvDesc.setText(desc);
    }

    /**
     * 选择勾选状态
     */
    public boolean isCheck() {
        return cbStatus.isChecked();
    }

    public void setCheck(boolean check) {
        cbStatus.setChecked(check);
        if(check) {
            setTvDesc(mDescOn);
        } else {
            setTvDesc(mDescOff);
        }
    }
}
